package com.junxonline.plat.service.repair.impl;

import com.junxonline.common.util.CommonUtils;
import com.junxonline.plat.dao.model.Device;
import com.junxonline.plat.dao.model.DeviceCheck;
import com.junxonline.plat.dao.model.User;
import com.junxonline.plat.dao.repository.DeviceCheckRepository;
import com.junxonline.plat.dao.repository.DeviceRepository;
import com.junxonline.plat.dao.repository.UserRepository;
import com.junxonline.plat.dao.vo.*;
import com.junxonline.plat.service.repair.RepairService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepairServiceImpl implements RepairService {

    private Logger logger = LogManager.getLogger();

    @Autowired
    private DeviceCheckRepository deviceCheckRepository;

    /**
     * 筛选查询设备
     *
     * @param pageVO
     * @param vo
     * @return
     */
    @Override
    public PageVO queryRepair(PageVO pageVO, QueryDeviceCheckVO vo) {

        PageRequest pageRequest = PageRequest.of(pageVO.getPage() - 1, pageVO.getLimit(), new Sort(Sort.Direction.DESC, "deviceCheckTime"));

        Page<DeviceCheck> page = deviceCheckRepository.findAll(new Specification<DeviceCheck>() {
            @Override
            public Predicate toPredicate(Root<DeviceCheck> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> params = new ArrayList<>();
                String deviceNo = vo.getDeviceNo();
                String deviceName = vo.getDeviceName();
                Integer deviceCheckStatus = vo.getDeviceCheckStatus();

                if (CommonUtils.isNotEmpty(deviceNo)) {
                    params.add(criteriaBuilder.equal(root.get("deviceNo"), deviceNo));
                }

                if (CommonUtils.isNotEmpty(deviceName)) {
                    params.add(criteriaBuilder.equal(root.get("deviceName"), deviceName));
                }

                if (CommonUtils.isNotEmpty(deviceCheckStatus)) {
                    params.add(criteriaBuilder.equal(root.get("deviceCheckStatus"), deviceCheckStatus));
                }

                return criteriaBuilder.and(params.toArray(new Predicate[params.size()]));
            }
        }, pageRequest);

        List<DeviceCheckVO> deviceCheckVOs = new ArrayList<>();
        List<DeviceCheck> deviceChecks = page.getContent();

        for (DeviceCheck deviceCheck : deviceChecks) {

            // 替换设备状态
            switch (deviceCheck.getDeviceCheckStatus()) {
                case 0:
                    deviceCheck.setDeviceCheckStatusString("<span class=\"layui-badge layui-bg-green\">正常</span>");
                    break;
                case 1:
                    deviceCheck.setDeviceCheckStatusString("<span class=\"layui-badge\">异常</span>");
                    break;
                default:
                    break;
            }

            DeviceCheckVO deviceCheckVO = new DeviceCheckVO();
            deviceCheckVO.setDeviceCheckId(deviceCheck.getDeviceCheckId());
            deviceCheckVO.setDeviceName(deviceCheck.getDeviceName());
            deviceCheckVO.setDeviceNo(deviceCheck.getDeviceNo());
            deviceCheckVO.setDeviceCheckTime(deviceCheck.getDeviceCheckTime());
            deviceCheckVO.setDeviceCheckDetail(deviceCheck.getDeviceCheckDetail());
            deviceCheckVO.setDeviceCheckStatus(deviceCheck.getDeviceCheckStatus());
            deviceCheckVO.setDeviceCheckStatusString(deviceCheck.getDeviceCheckStatusString());

            User createUser = deviceCheck.getCreateUser();
            if (CommonUtils.isNotEmpty(createUser)) {
                deviceCheckVO.setCreateUserId(createUser.getUserId());
                deviceCheckVO.setCreateUserName(createUser.getUsername());
            }

            User repairUser = deviceCheck.getRepairUser();
            if (CommonUtils.isNotEmpty(repairUser)) {
                deviceCheckVO.setRepairUserId(repairUser.getUserId());
                deviceCheckVO.setRepairUserName(repairUser.getUsername());
            }

            deviceCheckVOs.add(deviceCheckVO);

        }

        pageVO.setTotal(page.getTotalElements());
        pageVO.setData(deviceCheckVOs);

        return pageVO;

    }
    
}
